package com.zonda.sampleretrofit2.view;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.zonda.sampleretrofit2.Contants;
import com.zonda.sampleretrofit2.R;
import com.zonda.sampleretrofit2.data.AppNewsListApiClient;
import com.zonda.sampleretrofit2.data.NewsListService;
import com.zonda.sampleretrofit2.model.AppNewsListResult;
import com.zonda.sampleretrofit2.model.NewsModel;

import java.util.ArrayList;
import java.util.Collections;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MainFragment extends Fragment {

  private RecyclerView mRecyclerView;
  private NewsListAdapter mNewsListAdapter;

  public static MainFragment newInstance() {
    MainFragment fragment = new MainFragment();
    Bundle args = new Bundle();
    fragment.setArguments(args);
    return fragment;
  }

  public MainFragment() {
    // Required empty public constructor
  }

  private void loadData() {
    NewsListService service = AppNewsListApiClient.getNewsListService();
    Call<AppNewsListResult> dataCall = service.getNewsListDatas(Contants.DATA_SOURCE_URL2,
        Collections.<String, String>emptyMap());

    dataCall.enqueue(new Callback<AppNewsListResult>() {
      @Override
      public void onResponse(Call<AppNewsListResult> call, Response<AppNewsListResult> response) {

        int code = response.code();
        AppNewsListResult appNewsListResult = response.body();
        Log.i(Contants.LOG_TAG, "onResponse code: " + code);
        if (appNewsListResult != null) {
          ArrayList<NewsModel> listDatas = appNewsListResult.getListDatas();
          Log.i(Contants.LOG_TAG, " =========== response result size : " + listDatas.size()
              + " start =============");

          if (mRecyclerView != null) {
            mNewsListAdapter.setDatas(listDatas);
            mNewsListAdapter.notifyDataSetChanged();
          }
//          for (NewsModel newsModel :
//              listDatas) {
//            Log.i(Contants.LOG_TAG, " title: " + newsModel.getTitle());
//            ArrayList<NewsMediaModel> medias = newsModel.getThumbnailMedias();
//            if (medias != null && !medias.isEmpty()) {
//              Uri mediaUri = Uri.parse(medias.get(0).getmUrl());
//              Log.i(Contants.LOG_TAG, " medias start load");
//              Glide.with(Contants.APP_CONTEXT).load(mediaUri).asBitmap().into(new SimpleTarget<Bitmap>() {
//                @Override
//                public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
//                  Log.i(Contants.LOG_TAG, "onResourceReady resource width: " + resource.getWidth());
//                }
//              });
//            }
//          }
          Log.i(Contants.LOG_TAG, " =========== response result end =============");
        }
      }

      @Override
      public void onFailure(Call<AppNewsListResult> call, Throwable t) {
        Log.i(Contants.LOG_TAG, "onFailure Throwable: " + t.getMessage());
        Log.i(Contants.LOG_TAG, "onFailure Throwable: " + t.toString());
        for (StackTraceElement stackTraceElement :
            t.getStackTrace()) {
          Log.i(Contants.LOG_TAG, "onFailure stackTraceElement: " + stackTraceElement.toString());
        }
      }
    });
  }

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                           Bundle savedInstanceState) {

    Log.i(Contants.LOG_TAG, "onCreateView ");
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_main, container, false);
    mRecyclerView = (RecyclerView) view.findViewById(R.id.recycle_view);
//        mRecyclerView.setRecycledViewPool(new RecyclerView.RecycledViewPool());
    mRecyclerView.setRecyclerListener(new RecyclerView.RecyclerListener() {
      @Override
      public void onViewRecycled(RecyclerView.ViewHolder holder) {

      }
    });
    mNewsListAdapter = new NewsListAdapter();
    mRecyclerView.setLayoutManager(new LinearLayoutManager(view.getContext()));
    mRecyclerView.setAdapter(mNewsListAdapter);
    loadData();
    return view;
  }

}
